/* 
	@author:NIUNIU
	@date:2008-08-05
*/


/*	
	TickUnits,we will find the suitable max value from here.
	If you don't like these defaults, create your own instance of. 
	It must be plus and ascending chain.
	You can create another Units,just like this:
		var DecimalTickUnits=[
			{"value":0.0000001,"tickCount":2},
			{"value":0.000001,"tickCount":2}, 
			{"value":0.00001,"tickCount":2}, 
			{"value":0.0001,"tickCount":2},
			{"value":0.001,"tickCount":2},
			{"value":0.01,"tickCount":2},
			{"value":0.1,"tickCount":2},
			...
		]
*/
var IntegerTickUnits=[
{"value":1,"tickCount":2},
{"value":2,"tickCount":2},
{"value":5,"tickCount":5},
{"value":10,"tickCount":2},
{"value":20,"tickCount":2},
{"value":50,"tickCount":5},
{"value":100,"tickCount":2},
{"value":200,"tickCount":2},
{"value":500,"tickCount":5},
{"value":1000,"tickCount":2},
{"value":2000,"tickCount":2},
{"value":5000,"tickCount":5},
{"value":10000,"tickCount":2},
{"value":20000,"tickCount":2},
{"value":50000,"tickCount":5},
{"value":100000,"tickCount":2},
{"value":200000,"tickCount":2},
{"value":500000,"tickCount":5},
{"value":1000000,"tickCount":2},
{"value":2000000,"tickCount":2},
{"value":5000000,"tickCount":5},
{"value":10000000,"tickCount":2},
{"value":20000000,"tickCount":2},
{"value":50000000,"tickCount":5}
];

//looking for a suitable tickUnits.
Array.prototype.binarySearch = function(obj)
{  var value = 0;
   var left = 0;
   var right= this.length;
   
   if(obj<this[0].value){
		return this[0];
   }
   if(obj>this[this.length-1].value){
   		return this[this.length-1];
   }
   while(left <= right)
   {
      var center = Math.floor((left+right)/2);
      if(this[center].value==obj)
      {
         return this[center];
      }else if(obj < this[center].value)
      {
         right = center - 1;
         if(right>=0 && obj>this[right].value)
         {
         	return this[center];
         }
      }else
      {
         left = center + 1;
         if(left<this.length && obj <= this[left].value)
         {
         	return this[left];
         }
      }
   }
}

Array.prototype.max = function() {
	var max = this[0];
	for (var i = 1; i < this.length; i++) 
	{
		if (this[i] > max) 
			max = this[i];
	}
	if(max<0) return 0; 
	return Math.ceil(max);
}
Array.prototype.min = function() {
	var min = this[0];
	for (var i = 1; i < this.length; i++) 
	{	
		if (this[i] < min) 
			min = this[i];
	}
	if(min>0) return 0;
	return Math.floor(min);
}


function absMax(a,b){
	if(Math.abs(a)>=Math.abs(b)) return Math.abs(a);
	else return  Math.abs(b);
}

//compute the range,include zero.
function autoRange(valuesArr){
	var maxVal=valuesArr.max();
	var minVal=valuesArr.min();
	var pAbsMax = absMax(maxVal,minVal);
	//return a tick unit for integer values.
	var numberTick=IntegerTickUnits.binarySearch(pAbsMax);
	
	var tickCount=numberTick.tickCount;
	var steps=numberTick.value/numberTick.tickCount;
	if(Math.abs(maxVal)>=Math.abs(minVal)){
		maxVal=numberTick.value;
		if(minVal<0)
		{	
			minVal=Math.floor(minVal/steps)*steps;
		}
	}else{
		minVal=-numberTick.value;
		maxVal=Math.ceil(maxVal/steps)*steps;
	}	
	data.y_axis.max=maxVal;
	data.y_axis.min=minVal;
	data.y_axis.steps=steps;
}